<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar  @input="input" cancelButton="none" 
			 radius="100" placeholder="请输入内容" focus	>
			 </uni-search-bar>
		</view>
		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchList.length !== 0">
			<view class="sugg-list-item" v-for=" (item ,index) in searchList" :key="index" @click="gotoDetail(item)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="right" size="16"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-box-title">
				<text>搜素历史</text>
				<uni-icons size="17" type="trash" @click="clear"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-box-list">
				<uni-tag :text="item" v-for="(item,index) in histories" :key="index" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			timer: null,
			kw:'',//搜索关键词
			searchList: [],
			historyList: [],//搜索历史列表
		}
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			// 输入事件的处理函数
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(()=> {
					this.kw = e
					this.getSearchList()
				},500)
			},
			// 获取搜索列表方法
			async getSearchList() {
				// 判断搜索关键词是否为空
				if( this.kw === '') {
					this.searchList = []
					return
				} 
				const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query: this.kw})
				if(res.meta.status !== 200 ) return uni.$showMsg()
				this.searchList = res.message
				// 调用保存搜索关键词的方法
				this.saveSearchHistory()
				// 历史列表持久化
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			// 点击搜索列表跳转
			gotoDetail(item) {
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
					
				})
			},
			// 将关键词保存到历史列表的方法
			saveSearchHistory() {
				// this.historyList.push(this.kw)
				
				// 将array数组转化为set对象
				const set = new Set(this.historyList)
				// 调用set对象的delete方法,移除对应的元素
				set.delete(this.kw)
				// 调用set对象的add方法,向set中添加元素
				set.add(this.kw)
				// 将set对象转化为array数组
				this.historyList = Array.from(set)
			},
			// 点击删除图标的处理
			clear() {
				this.historyList = []
				uni.setStorageSync('kw', '[]')
			},
			// 点击历史搜索标签跳转页面
			gotoGoodsList(i){
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + i
				})
			}
		},
		computed: {
			histories() {
				return [...this.historyList].reverse()
			}
		},
	}
</script>

<style lang="scss">
.search-box {
	background-color: #c00000;
	position: sticky;
	top: 0;
	z-index: 999;
}

.sugg-list{
	padding: 0 5px;
	.sugg-list-item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 12px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		
		
		.goods-name{
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}
}
.history-box {
	padding: 0 15px;
	
	.history-box-title{
		display: flex;
		height: 40px;
		justify-content: space-between;
		align-items: center;
		font-size: 13px;
		border-bottom: 1px solid #efefef;
	}
	.history-box-list{
		display: flex;
		flex-warp: wrap;
		
		.uni-tag {
			margin: 5px  5px 0 0 ;
		}
	}
}
</style>
